package com.gxa.service.impl;

import com.gxa.common.R;
import com.gxa.mapper.SalesMapper;
import com.gxa.pojo.Goods_Save;
import com.gxa.pojo.Sales;
import com.gxa.pojo.Receipt_Sales;
import com.gxa.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SalesServiceImpl implements SalesService{
    R r=new R();
    @Autowired(required = false)
    SalesMapper SalesMapper;

    @Override
    public R inerstReceiptSales(Receipt_Sales receipt_Sales) {
        List<Sales> list = receipt_Sales.getSalesList();

        Integer result1 = SalesMapper.inerstReceiptSales(receipt_Sales);
        Integer result2 = SalesMapper.inerstSales(list);


        for (Sales item :list) {
            if("1".equals(receipt_Sales.getReceipt_Sales_Business_Type())){
                item.setSales_Number(0 - item.getSales_Number());
            }
            Goods_Save result3 = SalesMapper.queryGoodsSave(item.getWarehouse_Id(),item.getGoods_Id());
            if(result3 != null){
                Integer result4 = SalesMapper.updateGoodsSave(item.getWarehouse_Id(),item.getGoods_Id(),item.getSales_Number(),result3.getGoods_Save_Num());
            }else {
                Integer result5 = SalesMapper.insertGoodsSave(item.getWarehouse_Id(),item.getGoods_Id(),item.getSales_Number());
            }
        }
            r.setCode(200);
            r.setMsg("添加成功");
            r.setData(null);

        return r;
    }

    @Override
    public R auditReceiptSales(Map map) {
        Integer result = SalesMapper.auditReceiptSales(map);
        if(result != 0) {
            r.setCode(200);
            r.setMsg("审核成功");
            r.setData(null);
        }
        return r;
    }


}
